package com.tomorrow.runner.activity.history;

import android.app.LoaderManager;
import android.content.CursorLoader;
import android.content.Intent;
import android.content.Loader;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.tomorrow.runner.R;
import com.tomorrow.runner.activity.SportResultActivity;
import com.tomorrow.runner.activity.basic.BasicActivity;
import com.tomorrow.runner.data.model.ActivityModel;
import com.tomorrow.runner.util.Define;
import com.tomorrow.runner.util.TimeUitl;
import com.tomorrow.runner.widget.Ranger;

import org.joda.time.LocalDateTime;

import java.util.Calendar;
import java.util.GregorianCalendar;

public class HistoryActivity extends BasicActivity implements Ranger.DayViewOnClickListener, View.OnClickListener,LoaderManager.LoaderCallbacks<Cursor>, AdapterView.OnItemClickListener {

    private Ranger mDatePicker;
    private int mYear;
    private int mMonth;
    private int mDate;
    private ListView mHistoryList;
    private HistoryAdapter mAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_history);
        mDatePicker = (Ranger) findViewById(R.id.date_picker);
        mDatePicker.setDayViewOnClickListener(this);
        LocalDateTime localDateTime = new LocalDateTime();
        mYear = localDateTime.getYear();
        mMonth = localDateTime.getMonthOfYear();
        mDate = localDateTime.getDayOfMonth();
        mDatePicker.setStartAndEndDateWithParts(mYear,mMonth,1, mYear,mMonth, TimeUitl.getLastDayOfMonth(mYear,mMonth));
        mDatePicker.setSelectedDay(mDate,false,1000);
        mDatePicker.setOnControlButtonClickListener(this);
        initList();
    }

    private void initList() {
        mHistoryList = (ListView) findViewById(R.id.history_listView);
        mAdapter = new HistoryAdapter(this,null,true);
        getLoaderManager().restartLoader(0,null,this);
        mHistoryList.setAdapter(mAdapter);
        mHistoryList.setOnItemClickListener(this);
    }

    @Override
    public void onDaySelected(int day) {
        mDate = day;
        getLoaderManager().restartLoader(0,null,this);
    }

    @Override
    public void onClick(View v) {
        int tag = (int) v.getTag();
        switch (tag){
            case Ranger.TAG_LEFT_BUTTON:
                if (mMonth == Calendar.JANUARY){
                    mYear--;
                    mMonth = Calendar.DECEMBER;
                }else{
                    mMonth--;
                }
                mDatePicker.setStartAndEndDateWithParts(mYear,mMonth,1,mYear,mMonth,TimeUitl.getLastDayOfMonth(mYear,mMonth));
                break;
            case Ranger.TAG_RIGHT_BUTTON:
                if (mMonth == Calendar.DECEMBER){
                    mYear++;
                    mMonth = Calendar.JANUARY;
                }else{
                    mMonth++;
                }
                mDatePicker.setStartAndEndDateWithParts(mYear,mMonth,1,mYear,mMonth,TimeUitl.getLastDayOfMonth(mYear,mMonth));
                break;
        }
    }

    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        GregorianCalendar date = new GregorianCalendar(mYear,mMonth-1,mDate,0,0,0);
        String sql = ActivityModel.START_TIME + " BETWEEN "+ date.getTimeInMillis()+" AND " + (date.getTimeInMillis() + Define.ONE_DAY);
        CursorLoader cl = new CursorLoader(this, ActivityModel.getContentUri(), null, sql, null, null);
        cl.setUpdateThrottle(2000);
        return cl;
    }

    @Override
    public void onLoadFinished(Loader loader, Cursor data) {
        if (data == null || data.getCount()==0){
            findViewById(R.id.empty).setVisibility(View.VISIBLE);
        }
        else{
            findViewById(R.id.empty).setVisibility(View.GONE);
        }
        mAdapter.swapCursor(data);
    }

    @Override
    public void onLoaderReset(Loader loader) {

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (parent.getAdapter().getItem(position) instanceof Cursor){
            ActivityModel model = new ActivityModel((Cursor) parent.getAdapter().getItem(position));
            Intent intent = new Intent(this, SportResultActivity.class);
            Bundle data = new Bundle();
            data.putInt(Define.KEY_ID,model.getId());
            data.putInt(Define.KEY_TYPE, model.getInt(ActivityModel.ACTIVITY_TYPE));
            intent.putExtras(data);
            startActivity(intent);
        }
    }
}
